package io;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * @author Cheng Jun
 * @version 1.0
 * Description: 学习 OutPutStream，字节输出流，立刻写入文件
 * * @date 2019/10/25 14:16
 */
public class OutPutStreamExample {

    public static void main(String[] args) throws IOException {
        int b = 97;
        // write(b);

        byte[] bytes = {51, 52, 53, 54};
        // write(bytes);

        // int offset = 1;
        // int length = 3;
        // write(bytes, offset, length);

        // 追加写到文件末尾
        append(97);
    }

    /**
     *
     * @author Cheng Jun
     * @date 2022/8/13 21:46
     * @param bytes
     * @param offset
     * @param length
     * @return void
     * @see FileOutputStream#FileOutputStream(java.lang.String)
     */
    private static void write(byte[] bytes, int offset, int length) throws IOException {
        // java.io.FileOutputStream.FileOutputStream(java.lang.String) 构造函数write方法是覆盖原文件的内容
        OutputStream fileOutputStream = new FileOutputStream(".\\src\\io\\TestFile\\ForOutPutStreamExample.txt");
        fileOutputStream.write(bytes, offset, length);
        fileOutputStream.flush();
        fileOutputStream.close();
    }

    private static void write(byte[] bytes) throws IOException {
        OutputStream fileOutputStream = new FileOutputStream(".\\src\\io\\TestFile\\ForOutPutStreamExample.txt");
        fileOutputStream.write(bytes);
        fileOutputStream.close();
    }

    private static void write(int b) throws IOException {
        OutputStream fileOutputStream = new FileOutputStream(".\\src\\io\\TestFile\\ForOutPutStreamExample.txt");
        fileOutputStream.write(b);
        fileOutputStream.close();
    }

    /**
     *
     * @author Cheng Jun
     * @date 2022/8/13 21:48
     * @param num
     * @return void
     * @see FileOutputStream#FileOutputStream(java.lang.String, boolean)
     */
    private static void append(int num) throws IOException {
        FileOutputStream fileOutputStream = new FileOutputStream(".\\src\\io\\TestFile\\ForOutPutStreamExample.txt", true);
        fileOutputStream.write(num);
        fileOutputStream.close();
    }
}
